package com.sw.c.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.sw.c.annotations.SystemLogAnnotation;
import com.sw.c.common.Result;
import com.sw.c.common.ResultCodeEnum;
import com.sw.c.config.SystemParams;
import com.sw.c.mapper.UserRoleMapper;
import com.sw.c.pojo.entity.Permission;
import com.sw.c.pojo.entity.Role;
import com.sw.c.pojo.entity.User;
import com.sw.c.pojo.entity.UserRole;
import com.sw.c.pojo.vo.UserVo;
import com.sw.c.service.UserService;
import com.sw.c.utils.R;
import com.sw.c.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/menu")
@Api(tags = "01.角色管理")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private UserRoleMapper userRoleMapper;


    @SystemLogAnnotation(businessType = "用户登录",operationName = "用户登录")
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@RequestBody User user){
        return Result.success(userService.demo(user));
    }


    @SystemLogAnnotation(businessType = "获取数据",operationName = "角色菜单数据")
    @GetMapping("/getMenu")
    @ApiOperation("角色菜单数据")
    public R getPermission(Integer userId){
        if (userId == null) {
            return R.error("用户ID不可为空");
        }

        List<Role> roleList = userRoleMapper.getRoleId(userId);
        if (roleList.isEmpty()) {
            return R.error("该用户没有角色");
        }
        List<Integer> roleIdList = roleList.stream().map(Role::getId).collect(Collectors.toList());

        List<Permission> list = userService.getRolePermissionTree(0, roleIdList, userId);
        if (CollectionUtils.isEmpty(list)) {
            return R.error("该角色没有菜单");
        }

        return R.success(list);
    }

    @SystemLogAnnotation(businessType = "获取数据",operationName = "树菜单数据")
    @GetMapping("/getTree")
    @ApiOperation("树菜单数据")
    public R getTree(Integer userId){
        if (userId == null) {
            return R.error("用户ID不可为空");
        }
        List<Permission> list = userService.getTree(userId);
        if (CollectionUtils.isEmpty(list)) {
            return R.error("该角色没有菜单");
        }
        return R.success(list);
    }

    @SystemLogAnnotation(businessType = "获取树行菜单",operationName = "子项树菜单数据")
    @PostMapping("/getSubkeyTree")
    @ApiOperation("子项树菜单数据")
    public R getSubkeyTree(@RequestBody Map<String,Object> params){
        if (Objects.isNull(params.get("id"))) {
            return R.error("ID不可为空");
        }
        List<Permission> list = userService.getSubkeyTree(params);
        if (CollectionUtils.isEmpty(list)) {
            return R.error("没有菜单");
        }
        return R.success(list);
    }

    @SystemLogAnnotation(businessType = "修改",operationName = "修改角色")
    @PostMapping("/updateRole")
    @ApiOperation("修改角色")
    public R updateRole (@RequestParam Integer userId,Integer userRoleId) {
        if (userId == null) {
            return R.error("用户ID不可为空");
        }
        UserRole userRoleObjects = new UserRole();
        userRoleObjects.setRoleId(userRoleId);
        userRoleMapper.update(userRoleObjects, new UpdateWrapper<UserRole>().eq("user_id", userId));

        return R.success("修改角色成功");
    }

    @SystemLogAnnotation(businessType = "修改",operationName = "修改角色菜单")
    @PostMapping("/menu/{roleId}")
    @ApiOperation("修改角色菜单")
    public R updateRolePermission(@PathVariable Integer roleId, @RequestBody List<Integer> permissionIds) {

        if (roleId == null) {
            return R.error("角色ID不可为空");
        }

        String message = userService.updateRolePermission(roleId, permissionIds);

        return R.update(message);
    }

    @SystemLogAnnotation(businessType = "分页",operationName = "分页")
    @PostMapping("/page")
    @ApiOperation("分页")
    public R<IPage<User>> list( Integer pageNum,Integer pageSize) {


        IPage<User> page =  userService.pagelist(pageNum,pageSize);

        return R.success(page);
    }

}
